package com.bjpowernode.controller;

import com.bjpowernode.entity.TUser;
import com.bjpowernode.entity.TUserDO;
import com.bjpowernode.query.UserPwdQuery;
import com.bjpowernode.query.UserQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询用户
     *
     * @param current
     * @return
     */
    @GetMapping(value = "/api/users")
    public R userPage(@RequestParam(value = "current", required = false) Integer current) {
        // required = false 表示current参数可以传，也可以不传，也就是current参数可以是空的；
        // required = true 表示current参数必须传，不传会报错；
        if (current == null) {
            current = 1;
        }
        PageInfo<TUser> pageInfo = userService.getUserPage(current);
        return R.OK(pageInfo);
    }

    /**
     * 获取当前登录人的认证信息
     *
     * @param authentication
     * @return
     */
    @GetMapping(value = "/api/userInfo")
    public R userInfo(Authentication authentication) {
        return R.OK(authentication);
    }


    /**
     * 用户免登录
     *
     * @return
     */
    @GetMapping(value = "/api/autoLogin")
    public R autoLogin() {
        return R.OK();
    }

    /**
     * 根据用户id查询用户资料信息
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/api/user/{userId}")
    public R userInfo(@PathVariable(value = "userId") Integer userId) {
        TUser tUser = userService.getUserById(userId);
        return R.OK(tUser);
    }

    @GetMapping(value = "/api/user/loginPwd")
    public R checkLoginPwd(@RequestParam(value = "userId") Integer userId,
                           @RequestParam(value = "loginPwd") String loginPwd) {
        Boolean check = userService.checkLoginPwd(userId, loginPwd);
        return check ? R.OK() : R.FAIL();
    }

    /**
     * 修改密码
     *
     * @param userPwdQuery
     * @return
     */
    @PutMapping(value = "/api/user/pass")
    public R updatePwd(@RequestBody UserPwdQuery userPwdQuery) {
        int update = userService.updateLoginPwd(userPwdQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 添加用户
     *
     * @param userQuery
     * @return
     */
    @PostMapping(value = "/api/user")
    public R addUser(@Valid UserQuery userQuery) {
        int save = userService.saveUser(userQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 修改用户
     *
     * @param userQuery
     * @return
     */
    @PutMapping(value = "/api/user")
    public R editUser(@Valid UserQuery userQuery) {
        int update = userService.updateUser(userQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping(value = "/api/user/{id}")  // http://localhost:8088/api/user/23
    public R delUser(@PathVariable(value = "id") Integer userId) {
        int del = userService.delUserById(userId);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    // 测试 http://localhost:8080/api/order/1021/1
    @GetMapping(value = "/api/order/{id}/{status}")
    public R delUser222(@PathVariable(value = "id") Integer orderId, @PathVariable(value = "status") Integer status) {
        return R.OK();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/api/user")
    public R batchDelUser(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,8,12";
        List<String> idList = Arrays.asList(ids.split(","));
        int del = userService.batchDelUserByIds(idList);
        return del >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 查询所有负责人（查用户表）
     *
     * @return
     */
    @GetMapping(value = "/api/user/owner")
    public R owner() {
        List<TUserDO> tUserDOList = userService.getAllOwner();
        return R.OK(tUserDOList);
    }
}